module scene {

    export class TreasureBoxScene extends eui.Component {
        constructor(level) {
            super();
            this.level = Number(level)
            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "TreasureBoxPageSkin";
        }

        private addTreasureBnt: eui.Image
        private rejectBtn: eui.Label
        private lightMovie: egret.tween.TweenGroup
        private boxMovie: egret.tween.TweenGroup
        private starMovie: egret.tween.TweenGroup
        private starMovie2: egret.tween.TweenGroup
        private starMovie3: egret.tween.TweenGroup
        private starMovie4: egret.tween.TweenGroup
        private starMovie5: egret.tween.TweenGroup
        private openBoxCon: eui.Group
        private skill1: eui.Image
        private skill2: eui.Image
        private skill3: eui.Image
        private timer
        private timer2
        private timer3
        private level: number
        private bgImg: eui.Image

        private onComplete() {
            manager.StatisticsManager.sceneCount(1005)
            this.bgImg.width = manager.DisplayManager.STAGE_WIDTH;
            this.bgImg.height = manager.DisplayManager.STAGE_HEIGHT;

            this.rejectBtn.visible = false
            setTimeout(() => {
                this.rejectBtn.visible = true
            }, 500)

            this.addTreasureBnt.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAdd, this)
            this.rejectBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onReject, this)
            this.lightMovie.play(0)
            this.boxMovie.play(0)
            this.timer = setInterval(() => {
                this.lightMovie.play(0)
            }, 5000)
            this.timer3 = setInterval(() => {
                this.boxMovie.play(0)                
            }, 3750)

            this.timer2 = setInterval(() => {
                let random = Math.random()
                if(random <= 0.2){
                    this.starMovie.play(0)
                }else if(random <= 0.4) {
                    this.starMovie2.play(0)
                }else if(random <= 0.6) {
                    this.starMovie3.play(0)
                }else if(random <= 0.8) {
                    this.starMovie4.play(0)
                }else if(random <= 1) {
                    this.starMovie5.play(0)
                }
            }, 800)
        }

        private onAdd() {
            manager.StatisticsManager.click(151)
            console.log('level' + this.level)
            if(this.level <=2){
                this.onShareSucc()
                return
            }
            let adManager = manager.GameManager.getInstance().getAdManager();
            let revivalMethod = Number(adManager.ad_percent) - Math.random() * 100
            if(revivalMethod >= 0){
                adManager.showVideoAd();
                adManager.addEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
            }else{
                manager.ShareManager.getInstance().addEventListener(manager.ShareManager.SHARE_STATUS,
                    this.onShareStatus, this);
                manager.GameManager.getInstance().commonShare();
            }  
        }

        private onShareStatus(eve: egret.Event): void {
            manager.ShareManager.getInstance().removeEventListener(manager.ShareManager.SHARE_STATUS,
                this.onShareStatus, this);
            let succ: number = eve.data;
            if (succ > 0) {
                this.onShareSucc();
            }
        }

        private onVideoAdClose(eve: egret.Event): void {
            egret.log("视频广告播放事件:", eve.data);
            if (eve.data != null && eve.data.isEnded) {
                manager.GameManager.getInstance().adManager.removeEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
                this.onShareSucc();
            }
        }
        private image0: eui.Image 
        private onShareSucc(): void {

            this.addTreasureBnt.visible = false

            manager.SoundManager.playSound('openBoxMusic_wav')
            clearInterval(this.timer3)            
            egret.Tween.removeTweens(this.boxMovie);
            this.image0.visible = false
        
            let random = Math.random()
            if(random <= 0.4){
                this.skill1.visible = true
            }else if(random <= 0.8){
                this.skill2.visible = true 
            }else{
                this.skill3.visible = true
            }
            this.openBoxCon.visible = true

            setTimeout(() => {
                let event: events.AddTreasureBoxEvent = new events.AddTreasureBoxEvent(random);
                this.dispatchEvent(event)
            }, 1000)
        }

        private onReject() {
            manager.StatisticsManager.click(152)
            console.log('onReject')
            clearInterval(this.timer)
            clearInterval(this.timer2)
            clearInterval(this.timer3)
            let event: events.CloseTreasureBoxPageEvent  = new events.CloseTreasureBoxPageEvent();
            this.dispatchEvent(event)
        }
    }
}